﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain
{
    public class TargetCamera3D : ICamera3D, IBaseComponent, IDrawable
    {
        public Vector3 Up { get; set; }

        public Vector3 Position { get; set; }

        public Vector3 Target { get; set; }

        public bool ShouldDraw { get; set; }

        public Matrix View { get; set; }
        public Matrix Projection { get; set; }

        public string Name { get; set; }

        public BoundingFrustum Frustum { get; set; }
        public bool ShouldSerialize { get; set; }

        public bool GetAspectRatioFromEngineViewport { get; set; }

        public TargetCamera3D()
        {
            GetAspectRatioFromEngineViewport = true;

            NearPlane = 1;
            FarPlane = 1000;

            ShouldDraw = true;
            Frustum = new BoundingFrustum(View * Projection);
            ShouldSerialize = true;

            Up = Vector3.Up;
        }
        public TargetCamera3D(GameScreen gs)
            : this()
        {
            gs.AddComponent(this);
        }

        public float AspectRatio = 1;
        public float NearPlane { get; set; }
        public float FarPlane { get; set; }
        public float FieldOfView = MathHelper.ToRadians(45);

        public virtual void Draw()
        {
            float aspect = this.AspectRatio;
            if (GetAspectRatioFromEngineViewport)
            {
                aspect = Engine.Instance.GraphicsDevice.Viewport.AspectRatio;
            }

            Projection = CameraUtil.GetProjectionMatrix(FieldOfView, aspect, NearPlane, FarPlane);
            View = Matrix.CreateLookAt(this.Position, this.Target, Up);
            Frustum.Matrix = View * Projection;
        }

        public void SerializeData(SerializationData parentData, string index)
        {
        }

        public void DeserializeData(SerializationData parentData, string index)
        {
        }

        public void DeserializedAll()
        {
        }
    }
}
